// 三位数一个逗号
import {isEmpty} from "@/utils/is";

export function useAddCommas(num) {
	if(!num) {
		num = ''
	}
    return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
}


export function getTabsTop(el) { //el:节点
	return new Promise((resolve, reject) => {
		let observer = uni.createIntersectionObserver(this)	//创建并返回一个 IntersectionObserver 对象实例
		observer.relativeToViewport().observe(el, (res) => {
			resolve(res.boundingClientRect.top)//得到该el节点到视口顶部的距离
		})
	})
}

/**
 * 隐藏手机号码中间四位
 * @param phoneNumber {'手机号'}
 * @returns {string|boolean}
 */
export function useHiddenIntermediatePhoneNumber(phoneNumber) {
	if (phoneNumber.length !== 11) return false
	return `${phoneNumber.substring(0,3)} **** ${phoneNumber.substring(7)}`
}

/**
 *  转换日前为年月日
 * @param originDate
 * @returns {`${number}年${number}月${number}日`}
 */
export const useFormatDate = originDate => {
	const newDate = new Date(originDate)
	const year = newDate.getFullYear()
	const month = newDate.getMonth() + 1
	const day = newDate.getDate()

	return `${year}年${month}月${day}日`
}

export const useNewFormatDate = originDate => {
	const newDate = originDate
	const year = newDate.getFullYear()
	const month = (newDate.getMonth() + 1) > 9 ? newDate.getMonth() + 1 : '0' + newDate.getMonth() + 1
	const day = newDate.getDate() > 9 ? newDate.getDate() : '0' + newDate.getDate()
	return `${year}-${month}-${day}`
}


export const useFormatEmpty = val => {
	console.log(val)
	if(isEmpty(val)) return '暂无'
	else return val
}
